package com.ddxz.demo.clickhouse.task;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2021/7/15
 */

//@Component
public class DataLoadTask {

    @Scheduled(fixedDelay = 10)
    public void upload() {
        File file = new File(dataDir);
        if(!file.exists()) {
            return;
        }

        readData(file);
    }

    private void readData(File path) {
        File file;
        String line;
        List<String> row;
        Long startdate;
        for(String name : path.list()) {
            file = new File(name);
            if(!file.exists()) {
                continue;
            }

            try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
                while((line = reader.readLine()) != null) {
                    row = Arrays.asList(line.split("\\|"));
                    if(row.size() != 17) {
                        continue;
                    }
                    startdate = Optional.ofNullable(Long.parseLong(row.get(6))).orElse(0L);
                    row.add(String.valueOf(Math.floorDiv(startdate, 1000L) * 1000L));
                    row.add(String.valueOf(Math.floorDiv(startdate, 60000L) * 60000L));
                    row.add(String.valueOf(Math.floorDiv(startdate, 300000L) * 300000L));
                    row.add(String.valueOf(Math.floorDiv(startdate, 600000L) * 600000L));
                    row.add(String.valueOf(Math.floorDiv(startdate, 900000L) * 900000L));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    @Value("${eversec.file.path}")
    private String dataDir;
}
